package com.wantupai.mvp.mvp.lce.fragment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.v4.app.Fragment;
import android.view.View;

import com.wantupai.mvp.framework.support.MvpDelegateCallback;
import com.wantupai.mvp.framework.support.fragment.FragmentMvpDelegateImpl;
import com.wantupai.mvp.mvp.MvpPresenter;
import com.wantupai.mvp.mvp.MvpView;
import com.wantupai.mvp.mvp.lce.MvpViewImpl;


/**
 * 第一重代理->生命周期->代理对象
 * 第二重代理->目标对象presenter和view的创建和绑定
 *
 * @param <V>
 * @param <P>
 * @author :zhangqing
 */
public class MvpFragment<
        D,
        V extends MvpView<D>,
        P extends MvpPresenter<V>>
        extends Fragment implements
        MvpView<D>, MvpDelegateCallback<V, P> {

    //持有目标对象引用
    protected FragmentMvpDelegateImpl<V, P> mvpDelegate;
    protected MvpViewImpl<D> mDMvpViewimpl;

    protected FragmentMvpDelegateImpl<V, P> getMvpDelegate() {
        if (this.mvpDelegate == null) {
            this.mvpDelegate = new FragmentMvpDelegateImpl<V, P>(this);
        }
        return mvpDelegate;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        getMvpDelegate().onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getMvpDelegate().onCreate(savedInstanceState);
    }

    public MvpViewImpl<D> getDMvpViewimpl() {
        if (mDMvpViewimpl == null) {
            mDMvpViewimpl = new MvpViewImpl();
        }
        return mDMvpViewimpl;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getMvpDelegate().onActivityCreated(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getMvpDelegate().onViewCreated(view, savedInstanceState);
        getDMvpViewimpl().initView(view);
        getDMvpViewimpl().setOnClickErrorListener(this::onErrorClick);
    }

    public void Login(View view) {
        toLogin();
    }

    public void toLogin() {

    }


    public void setEmptyImageView(@DrawableRes int imageResourcesId) {
        getDMvpViewimpl().setEmptyImageView(imageResourcesId);
    }

    public void setEmptyTextView(CharSequence charSequence) {
        getDMvpViewimpl().setEmptyTextView(charSequence);
    }

    public void onErrorClick(View view) {
        loadData(false);
    }

    @Override
    public void onStart() {
        super.onStart();
        getMvpDelegate().onStart();
    }

    @Override
    public void onPause() {
        super.onPause();
        getMvpDelegate().onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        getMvpDelegate().onResume();
    }

    @Override
    public void onStop() {
        super.onStop();
        getMvpDelegate().onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getMvpDelegate().onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getMvpDelegate().onDestroy();

    }

    @Override
    public void onDetach() {
        super.onDetach();
        getMvpDelegate().onDetach();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getMvpDelegate().onSaveInstanceState(outState);
    }

    private P presenter;

    @Override
    public P createPresenter() {
        return presenter;
    }

    @Override
    public P getPresenter() {
        return presenter;
    }

    @Override
    public void setPresenter(P p) {
        presenter = p;
    }

    @Override
    public V getMvpView() {
        return (V) this;
    }


    @Override
    public void showLoading(boolean isPullToRefresh) {
        getDMvpViewimpl().showLoading(isPullToRefresh);
    }

    @Override
    public void loadData(boolean isPullToRefresh) {
        getDMvpViewimpl().loadData(isPullToRefresh);
    }

    @Override
    public void showContent() {
        getDMvpViewimpl().showContent();
    }

    @Override
    public void bindData(D d) {
        getDMvpViewimpl().bindData(d);
    }

    @Override
    public void showError() {
        getDMvpViewimpl().showError();
    }


    @Override
    public void showEmpty() {
        getDMvpViewimpl().showEmpty();
    }

    private Dialog mDialog;

    @Override
    public void showLoadingDialog() {
        if (mDialog == null) {
            //     mDialog = LoadingDialog.builder(getActivity());
        }
        //mDialog.show();
    }

    @Override
    public void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    @Override
    public void showNetWorkError() {
        //new ToastDialog.Builder(getActivity()).setContent("网络不好，请检查您的网络设置").show();
    }

}
